﻿namespace WebServiceExtensions.Configurations.Common
{
	using System;
	using System.ComponentModel;
	using System.Configuration;
	using System.Globalization;

	/// <summary>
	/// Converter to convert string to Type.
	/// </summary>
	public class StringToTypeConverter : ConfigurationConverterBase
	{
		#region Methods (2) 

		// Public Methods (2) 

		/// <summary>
		/// Converts from.
		/// </summary>
		/// <param name="ctx">Type descriptor context.</param>
		/// <param name="ci">Culture info</param>
		/// <param name="data">The object.</param>
		/// <returns>the type object</returns>
		public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data)
		{
			string typeData = (string)data;
			Type type = Type.GetType(typeData);
			return type;
		}

		/// <summary>
		/// Converts to.
		/// </summary>
		/// <param name="ctx">Type descriptor context.</param>
		/// <param name="ci">Culture info</param>
		/// <param name="value">The value.</param>
		/// <param name="type">The type.</param>
		/// <returns>the object</returns>
		public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
		{
			return ((Type)value).AssemblyQualifiedName;
		}

		#endregion Methods 
	}
}